using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Chimera2.Classes;

namespace Chimera2.GUI
{
    public partial class Bulletins : Form
    {
        Bulletin bulletin = new Bulletin();
        List<Hub> hubs = new List<Hub>();

        // define delegate
        public delegate void PassControl(object sender);

        // create instance (null)
        public PassControl passControl;

        public Bulletins()
        {
        }

        public Bulletins(Bulletin _bulletin, Dictionary<Guid, Hub> _hubs)
        {
            InitializeComponent();
            bulletin = _bulletin;
            hubs = new List<Hub>(_hubs.Values);
            OnLoad();
        }

        private void OnLoad()
        {
            txtBulletinName.Text = bulletin.BulletinName;
            txtTextcommandName.Text = bulletin.TextCmdName;
            rdbAllowAccessFromGlobal.Checked = bulletin.GlobalAccess;
            rdbAllowAccessFromList.Checked = (!bulletin.GlobalAccess);
            clbAllowAccessFromList.Enabled = (!bulletin.GlobalAccess);

            foreach (Hub hub in hubs)
            {
                if (bulletin.AccessFrom.Contains(hub.Guid))
                {
                    clbAllowAccessFromList.Items.Add(hub.HubName, true);
                }
                else
                {
                    clbAllowAccessFromList.Items.Add(hub.HubName, false);
                }
            }

            chkRegularCanAdd.Checked = bulletin.RegularCanAdd;
            chkRegularCanEdit.Checked = bulletin.RegularCanEdit;
            numRegularCanEditMinutes.Enabled = bulletin.RegularCanEdit;
            numRegularCanEditMinutes.Value = bulletin.RegularEditMinutes;            
            chkAnnounceNewPosts.Checked = bulletin.AnnounceNew;
            chkOperatorsAreEditors.Checked = bulletin.OpsAreEditors;
        }

        private void OnSave()
        {
            bulletin.BulletinName = txtBulletinName.Text;
            bulletin.TextCmdName = txtTextcommandName.Text;
            bulletin.GlobalAccess = rdbAllowAccessFromGlobal.Checked;

            bulletin.AccessFrom.Clear();
            foreach (int index in clbAllowAccessFromList.CheckedIndices)
            {
                bulletin.AccessFrom.Add(hubs[index].Guid);
            }

            bulletin.RegularCanAdd = chkRegularCanAdd.Checked;
            bulletin.RegularCanEdit = chkRegularCanEdit.Checked;
            bulletin.RegularEditMinutes = (int)numRegularCanEditMinutes.Value;
            bulletin.AnnounceNew = chkAnnounceNewPosts.Checked;
            bulletin.OpsAreEditors = chkOperatorsAreEditors.Checked;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtBulletinName.Text) || string.IsNullOrEmpty(txtTextcommandName.Text))
            {
                MessageBox.Show("'Bulletin name' and 'Textcommand name' is required.", "Bulletin", MessageBoxButtons.OK);
            }
            else
            {
                OnSave();
                passControl(bulletin);
                this.Close();
            }
        }

        private void rdbAllowAccessFromList_CheckedChanged(object sender, EventArgs e)
        {
            clbAllowAccessFromList.Enabled = rdbAllowAccessFromList.Checked;
        }

        private void chkRegularCanEdit_CheckedChanged(object sender, EventArgs e)
        {
            numRegularCanEditMinutes.Enabled = chkRegularCanEdit.Checked;
        }
    }
}